//
//  CompanyListTableViewController.m
//  Express
//
//  Created by lanou on 16/1/9.
//  Copyright © 2016年 FJY. All rights reserved.
//

#import "CompanyListTableViewController.h"
#import "BaseHeader.h"

@interface CompanyListTableViewController ()

@property (nonatomic, strong) NSMutableArray *keyArray;
@property (nonatomic, strong) NSMutableDictionary *modelDic;
@property (nonatomic, strong) NSMutableDictionary *rootDic;

@end

@implementation CompanyListTableViewController

- (NSMutableArray *)keyArray {
    if (!_keyArray) {
        _keyArray = [[NSMutableArray alloc] init];
    }
    return _keyArray;
}

- (NSMutableDictionary *)modelDic {
    if (!_modelDic) {
        _modelDic = [[NSMutableDictionary alloc] init];
    }
    return _modelDic;
}

- (NSMutableDictionary *)rootDic {
    if (!_rootDic) {
        _rootDic = [[NSMutableDictionary alloc] init];
    }
    return _rootDic;
}

- (void)setCompanyData {
    NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"ExpressCode" ofType:@"plist"];
    self.rootDic = [NSMutableDictionary dictionaryWithContentsOfFile:plistPath];
    self.keyArray = [[self.rootDic.allKeys sortedArrayUsingSelector:@selector(compare:)] mutableCopy];
    [self.keyArray insertObject:self.keyArray.lastObject atIndex:0];
    [self.keyArray removeLastObject];
    for (NSString *key in self.rootDic) {
        NSMutableArray *companies = [NSMutableArray array];
        for (NSDictionary *company in self.rootDic[key]) {
            Express *expressCompany = [[Express alloc] init];
            [expressCompany setValuesForKeysWithDictionary:company];
            [companies addObject:expressCompany];
        }
        [self.modelDic setValue:companies forKey:key];
    }
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    
    [self setCompanyData];
    
    [self.tableView registerClass:[TelTableViewCell class] forCellReuseIdentifier:@"telCell"];
    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (Express *)expressCompanyWithIndexPath:(NSIndexPath *)indexPath {
    return self.modelDic[self.keyArray[indexPath.section]][indexPath.row];
}

- (NSArray *)expressCompaniesWithIndexPath:(NSIndexPath *)indexPath {
    return self.modelDic[self.keyArray[indexPath.section]];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.keyArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.rootDic[self.keyArray[section]] count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    TelTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"telCell" forIndexPath:indexPath];
    Express *currentEC = [self expressCompanyWithIndexPath:indexPath];
    cell.express = currentEC;
    
    return cell;
}

- (NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView {
    return self.keyArray;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    return self.keyArray[section];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 60;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    //tel
//    NSMutableString *string = [[NSMutableString alloc] initWithFormat:@"tel://%@",[self expressCompanyWithIndexPath:indexPath].expressCode];
//    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:string]];
    
    Express *currentExpress = [self expressCompanyWithIndexPath:indexPath];
    [self.delegate returnWithExpressCompanyCode:[self expressCompanyWithIndexPath:indexPath].expressCode companyName:currentExpress.expressName tel:currentExpress.expressTelephone];
    [self.navigationController popViewControllerAnimated:YES];
}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
